package com.jinlongchen.api;

import com.jinlongchen.util.DBUtil;
import com.jinlongchen.service.AlbumService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

@WebServlet("/api/album-detail.json")
public class AlbumDetailApi extends AbsApiServlet {
    private final AlbumService albumService = new AlbumService();
    @Override
    protected Object doGetInner(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException, APIException {
        int aid = Integer.parseInt(req.getParameter("aid"));

        try (Connection c = DBUtil.getConnection()){
            return albumService.get(c,aid);
        }
//        Album album = new Album();
//        album.aid = aid;
//        album.storyList = new ArrayList<>();
//
//        try (Connection c = DBUtil.getConnection()) {
//            {
//                String sql = "SELECT name,cover FROM album WHERE aid = ?";
//                try (PreparedStatement s = c.prepareStatement(sql)) {
//                    s.setInt(1, aid);
//                    try (ResultSet rs = s.executeQuery()) {
//                        if (!rs.next()) {
//                            //404
//                            throw new APIException("aid 对应的专辑不存在");
//                        }
//
//                        album.name = rs.getString("name");
//                        album.cover = rs.getString("cover");
//                    }
//                }
//            }
//
//            {
//                String sql = "SELECT sid,name FROM story WHERE aid = ? ORDER BY sid";
//                try (PreparedStatement s = c.prepareStatement(sql)) {
//                    s.setInt(1, aid);
//                    try (ResultSet rs = s.executeQuery()) {
//                        while (rs.next()) {
//                            Story story = new Story();
//                            story.sid = rs.getInt("sid");
//                            story.name = rs.getString("name");
//
//                            album.storyList.add(story);
//                        }
//                    }
//                }
//            }
//        }
//        return album;
    }

//    private final ObjectMapper objectMapper = new ObjectMapper();
//    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        //1、读取aid
//        req.setCharacterEncoding("utf-8");
//        int aid = Integer.parseInt(req.getParameter("aid"));
//        //一个Connection 对象可以用于多次查询(Statement 的执行)
//        //2、从 album 表中查询 该专辑的元信息(name,cover) SELECT name,cover FROM album WHERE aid=?;
//        //3、从story 表中查询属于该专辑的故事列表(sid,name) SELECT sid,name FROM story WHERE aid=? ORDER BY sid;
//        // 2 + 3 可以直接通过联表查询(JOIN)完成
//        //SELECT a.name,cover,sid,s.name FROM album a JOIN story s ON a.aid = s.aid WHERE a.aid=? ORDER BY sid;
//        try {
//            Album album = new Album();
//            album.aid = aid;
//            album.storyList = new ArrayList<>();
//
//            try (Connection c = DBUtil.getConnection()){
//                {
//                    String sql = "SELECT name,cover FROM album WHERE aid = ?";
//                    try (PreparedStatement s = c.prepareStatement(sql)){
//                        s.setInt(1,aid);
//                        try (ResultSet rs = s.executeQuery()){
//                            if (!rs.next()){
//                                //404
//                                Object result= new Object(){
//                                    public boolean success = false;
//                                    public String reason = "aid 对应的专辑不存在";
//                                };
//                                //5、序列化JSON
//                                String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(result);
//                                //6、写响应
//                                resp.setCharacterEncoding("utf-8");
//                                resp.setContentType("application/json");
//                                resp.getWriter().println(json);
//                                return;
//                            }
//
//                            album.name = rs.getString("name");
//                            album.cover = rs.getString("cover");
//                        }
//
//                    }
//                }
//
//                {
//                    String sql = "SELECT sid,name FROM story WHERE aid = ? ORDER BY sid";
//                    try (PreparedStatement s = c.prepareStatement(sql)){
//                        s.setInt(1,aid);
//                        try (ResultSet rs = s.executeQuery()){
//                            while (rs.next()){
//                                Story story = new Story();
//                                story.sid = rs.getInt("sid");
//                                story.name = rs.getString("name");
//
//                                album.storyList.add(story);
//                            }
//                        }
//                    }
//                }
//            }
//
//            //4、根据API格式构建返回对象
//            Object result = new Object(){
//                public boolean success = true;
//                public Object data = album;
//            };
//            //5、序列化JSON
//            String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(result);
//            //6、写响应
//            resp.setCharacterEncoding("utf-8");
//            resp.setContentType("application/json");
//            resp.getWriter().println(json);
//        }catch (SQLException exc){
//            exc.printStackTrace();
//            Object result = new Object(){
//                public boolean success = false;
//                public String reason = exc.getMessage();
//            };
//            //5、序列化JSON
//            String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(result);
//            //6、写响应
//            resp.setCharacterEncoding("utf-8");
//            resp.setContentType("application/json");
//            resp.getWriter().println(json);
//        }
//
//    }
}
